/*
 * To change this license header, choose License Headers in Project Properties.
 * To change this template file, choose Tools | Templates
 * and open the template in the editor.
 */

package server.boundary;

import server.entities.Account;
import server.entities.Transaction;
import server.entities.User;
import java.util.List;
import java.util.Map;
import javax.ejb.Stateless;
import javax.persistence.EntityManager;
import javax.persistence.PersistenceContext;
import javax.persistence.TypedQuery;
import javax.persistence.criteria.CriteriaQuery;
import javax.persistence.criteria.Root;
import types.ResponseXML;

/**
 *
 * @author efim
 */
@Stateless
public class AccountFacade extends AbstractFacade<Account> {
    @PersistenceContext(unitName = "EFMPU")
    private EntityManager em;

    @Override
    protected EntityManager getEntityManager() {
        return em;
    }

    public AccountFacade() {
        super(Account.class);
    }
    
    public List<Account> getAccounts(Map<String,String> params)
    {
        CriteriaQuery cq = getEntityManager().getCriteriaBuilder().createQuery();        
        Root<Account> account = cq.from(Account.class);
        for(String p:params.keySet())
        {
            switch(p)
            {                
                case "user_id":  
                    cq = cq.where(account.get("userId").in(Long.parseLong(params.get(p))));
                    break; 
                case "id":  
                    cq = cq.where(account.get("id").in(params.get(p)));
                    break;
                case "name":  
                    cq = cq.where(account.get("name").in(params.get(p)));
                    break;
            }
        }        
        TypedQuery q = getEntityManager().createQuery(cq);
        List list = q.getResultList();
        return list;
    }

    public Account createAccount(Map<String, String> params) {
        Account account = new Account();
        for(String p:params.keySet())
        {
            switch(p)
            {                
                case "name":
                    account.setName(params.get(p));
                    break;
                case "user_id":              
                    account.setUserId(Long.parseLong(params.get(p)));
                    break;
                                                   
            }
        }
        
        this.create(account);
        return account;       
    }
    
}
